package com.huaxin.hxmoduleworkorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaxin.feignclient.file.models.Base64ImageUploadReqDTO;
import com.huaxin.feignclient.file.models.UploadFIleRespDTO;
import com.huaxin.hxmoduleworkorder.dto.request.MaintenanceOrderElectricianTaskResultCreateReqDTO;
import com.huaxin.hxmoduleworkorder.dto.request.MaintenanceOrderElectricianTaskResultUpdateReqDTO;
import com.huaxin.hxmoduleworkorder.feignclients.FileFeignService;
import com.huaxin.hxmoduleworkorder.mapper.MaintenanceOrderElectricianTaskResultMapper;
import com.huaxin.hxmoduleworkorder.mapping.MaintenanceOrderElectricianTaskResultMapping;
import com.huaxin.hxmoduleworkorder.models.MaintenanceOrderElectricianTaskResult;
import com.huaxin.hxmoduleworkorder.service.MaintenanceOrderElectricianTaskResultService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class MaintenanceOrderElectricianTaskResultServiceImpl extends ServiceImpl<MaintenanceOrderElectricianTaskResultMapper, MaintenanceOrderElectricianTaskResult> implements MaintenanceOrderElectricianTaskResultService {
    private final ApplicationContext applicationContext;
    private final MaintenanceOrderElectricianTaskResultMapping mapping;
    private final FileFeignService fileFeignService;

    @Override
    public Boolean create(MaintenanceOrderElectricianTaskResultCreateReqDTO in) {
        if (in == null) {
            return false;
        }
        if (StringUtils.isBlank(in.getMaintenanceOrderId())) {
            return false;
        }
        if (StringUtils.isBlank(in.getElectricianId())) {
            return false;
        }
        if (StringUtils.isBlank(in.getProblemDescription())) {
            return false;
        }
        if (StringUtils.isBlank(in.getSolution())) {
            return false;
        }
        if (StringUtils.isBlank(in.getSignImageUrl()) && StringUtils.isBlank(in.getSignImageBase64Data())) {
            return false;
        }
        MaintenanceOrderElectricianTaskResult entity = mapping.toEntity(in);

        // 处理签名图片，SignImageUrl赋值时直接保存，否则上传SignImageBase64Data
        String signImageUrl = in.getSignImageUrl();
        if (StringUtils.isBlank(signImageUrl)) {
            signImageUrl = this.getSignImageUrl(in.getSignImageBase64Data());
        }
        if (signImageUrl == null || signImageUrl.isEmpty()) {
            return false;
        }
        entity.setSignImageUrl(signImageUrl);
        return this.save(entity);
    }

    @Override
    public Boolean update(MaintenanceOrderElectricianTaskResultUpdateReqDTO in) {
        if (in == null) {
            return false;
        }
        MaintenanceOrderElectricianTaskResult entity = this.getById(in.getId());
        if (entity == null) {
            return false;
        }
        if (StringUtils.isBlank(in.getMaintenanceOrderId())) {
            return false;
        }
        if (StringUtils.isBlank(in.getElectricianId())) {
            return false;
        }
        if (StringUtils.isBlank(in.getProblemDescription())) {
            return false;
        }
        if (StringUtils.isBlank(in.getSolution())) {
            return false;
        }
        this.mapping.updateEntity(in, entity);
        String signImageUrl = in.getSignImageUrl();
        if (signImageUrl == null || StringUtils.isBlank(signImageUrl)) {
            signImageUrl = this.getSignImageUrl(in.getSignImageBase64Data());
        }
        if (signImageUrl == null || signImageUrl.isEmpty()) {
            return false;
        }
        entity.setSignImageUrl(signImageUrl);
        return this.updateById(entity);
    }

    public MaintenanceOrderElectricianTaskResult getByElectricianTaskId(String taskId) {
        LambdaQueryWrapper<MaintenanceOrderElectricianTaskResult> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MaintenanceOrderElectricianTaskResult::getMaintenanceOrderElectricianTaskId, taskId);
        return this.getBaseMapper().selectOne(wrapper);
    }

    // 处理签名图片base64数据
    private String getSignImageUrl(String signImageBase64Data) {
        if (StringUtils.isBlank(signImageBase64Data)) {
            return null;
        }
        Base64ImageUploadReqDTO Base64ImageUploadReqDTO = new Base64ImageUploadReqDTO();
        Base64ImageUploadReqDTO.setBase64Data(signImageBase64Data);
        UploadFIleRespDTO uploadFIleRespDTO = fileFeignService.uploadBase64Image(Base64ImageUploadReqDTO);
        return uploadFIleRespDTO != null ? uploadFIleRespDTO.getFileUrl() : null;
    }
}